package com.wq.babysecretary.ui.activity;


import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.babysecretary.R;
import com.wq.babysecretary.base.UeBaseActivity;
import com.wq.babysecretary.ui.fragment.HomeFragment;
import com.wq.babysecretary.ui.fragment.KnowlageFragment;
import com.wq.babysecretary.weight.ShouYeViewPager;
import com.wq.babysecretary.weight.runtimepermissions.PermissionsManager;
import com.wq.babysecretary.weight.runtimepermissions.PermissionsResultAction;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends UeBaseActivity implements View.OnClickListener{
    private ShouYeViewPager mViewPger;
    private List<Fragment> fragments = new ArrayList<>();
    private HomeFragment homeFragment=new HomeFragment();
    private KnowlageFragment knowlageFragment=new KnowlageFragment();
    private LinearLayout footer1,footer2,footer3;
    private ImageView footer1_img,footer2_img,footer3_img;

    private TextView footer1_text,footer2_text,footer3_text;
    private long currTimemill;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_main);
        setTitleText("首页");
        initView();
    }
    private void initView(){
        mViewPger = (ShouYeViewPager) findViewById(R.id.mViewPager);
        fragments.add(homeFragment);
        fragments.add(knowlageFragment);
        ViewPagerAdapter mainAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPger.setAdapter(mainAdapter);
        mViewPger.addOnPageChangeListener(mainAdapter);
        mViewPger.setCurrentItem(0);
        footer1=(LinearLayout)findViewById(R.id.footer1);
        footer2=(LinearLayout)findViewById(R.id.footer2);
        footer3=(LinearLayout)findViewById(R.id.footer3);
        footer1.setOnClickListener(this);
        footer2.setOnClickListener(this);
        footer3.setOnClickListener(this);
        footer1_img=(ImageView)findViewById(R.id.footer1_img);
        footer2_img=(ImageView)findViewById(R.id.footer2_img);
        footer3_img=(ImageView)findViewById(R.id.footer3_img);
        footer1_text=(TextView)findViewById(R.id.footer1_text);
        footer2_text=(TextView)findViewById(R.id.footer2_text);
        footer3_text=(TextView)findViewById(R.id.footer3_text);
        requestPermissions();
    }
    @TargetApi(23)
    private void requestPermissions() {
        PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() {
            @Override
            public void onGranted() {
//				Toast.makeText(MainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDenied(String permission) {

                //Toast.makeText(MainActivity.this, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.footer1:
//                footer1_img.setImageResource(R.mipmap.bar_shelf_hover);
//                footer2_img.setImageResource(R.mipmap.bar_store_normal);
//                footer3_img.setImageResource(R.mipmap.bar_set_normal);
                footer1_text.setTextColor(Color.parseColor("#FE7AA2"));
                footer2_text.setTextColor(Color.parseColor("#898989"));
                footer3_text.setTextColor(Color.parseColor("#898989"));
                switchUI(0);
                break;
            case R.id.footer2:
//                footer1_img.setImageResource(R.mipmap.bar_shelf_normal);
//                footer2_img.setImageResource(R.mipmap.bar_store_hover);
//                footer3_img.setImageResource(R.mipmap.bar_set_normal);
                footer1_text.setTextColor(Color.parseColor("#898989"));
                footer2_text.setTextColor(Color.parseColor("#FE7AA2"));
                footer3_text.setTextColor(Color.parseColor("#898989"));
                switchUI(1);
                break;
            case R.id.footer3:
//                footer1_img.setImageResource(R.mipmap.bar_shelf_normal);
//                footer2_img.setImageResource(R.mipmap.bar_store_normal);
//                footer3_img.setImageResource(R.mipmap.bar_set_hover);
//                footer1_text.setTextColor(Color.parseColor("#898989"));
//                footer2_text.setTextColor(Color.parseColor("#898989"));
//                footer3_text.setTextColor(Color.parseColor("#1089DC"));
//                switchUI(2);
                break;
        }
    }

    public class ViewPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener{


        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    private void switchUI(int index){
        switch (index){
            case 0:
                hiddenHeader();
                mViewPger.setCurrentItem(0);
                break;
            case 1:
                hiddenHeader();
                setTitleText("知识圈");
                mViewPger.setCurrentItem(1);
                break;
            case 2:
                hiddenHeader();
                setTitleText("设置");
                mViewPger.setCurrentItem(2);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long time = System.currentTimeMillis();
            if ((time - currTimemill) > 1500) {
                Toast.makeText(this,"再次点击退出萌宝小秘",Toast.LENGTH_SHORT).show();
                currTimemill = time;
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
